package com.ykzhujiang.lovespirit;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ykzhujiang.lovespirit.entity.LThread;
import com.ykzhujiang.lovespirit.entity.Notice;
import com.ykzhujiang.lovespirit.parser.FudanParser;
import com.ykzhujiang.lovespirit.parser.NanjingParser;
import com.ykzhujiang.lovespirit.parser.NankaiParser;
import com.ykzhujiang.lovespirit.parser.Parser;
import com.ykzhujiang.lovespirit.parser.SDUParser;
import com.ykzhujiang.lovespirit.parser.SJTUParser;
import com.ykzhujiang.lovespirit.parser.TJUParser;
import com.ykzhujiang.lovespirit.parser.TsinghuaParser;
import com.ykzhujiang.lovespirit.parser.XJTUParser;
import com.ykzhujiang.lovespirit.util.DataManager;
import com.ykzhujiang.lovespirit.util.ParserProcessor;
import com.ykzhujiang.lovespirit.util.Utility;

@SuppressWarnings("serial")
public class DeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		int start = 0;
		boolean flag = true;
		try {
			start = Integer.parseInt(req.getParameter("s"));
		} catch (NumberFormatException e) {
			flag = false;
		}
		if (start < 0)
			start = 0;
		if (!flag)
			// play a trick, need to be fixed
			start = (int) (Math.random() * 1000);
		logger.info("try to delete notices fool:" + start);
		String query = "select from " + Notice.class.getName()
				+ " where type == " + Notice.TYPE_NORMAL
				+ " order by hot desc range " + start + ", "
				+ (start + DELETE_NUM_ONCE);
		List<Notice> results = (List<Notice>) DataManager.getData(query, false,
				null);
		for (int i = 0; i < results.size(); i++) {
			Notice notice = results.get(i);
			Parser p = null;
			String url = notice.getSourceUrl().toLowerCase();
			if (url.indexOf("bbs.sjtu") != -1)
				p = new SJTUParser();
			else if (url.indexOf("bbs.xjtu") != -1)
				p = new XJTUParser();
			else if (url.indexOf("bbs.fudan") != -1)
				p = new FudanParser();
			else if (url.indexOf("bbs.nju") != -1)
				p = new NanjingParser();
			else if (url.indexOf("bbs.tsinghua") != -1)
				p = new TsinghuaParser();
			else if (url.indexOf("bbs.nankai") != -1)
				p = new NankaiParser();
			else if (url.indexOf("bbs.sdu") != -1)
				p = new SDUParser();
			else if (url.indexOf("bbs.tju") != -1)
				p = new TJUParser();
			else if (p == null)
				continue;
			String text = Utility.GetURLText(notice.getSourceUrl(),
					ParserProcessor.connect_times);
			if (text == null)
				continue;
			LThread lt = p.Parse(notice.getSourceUrl(), text);
			if (lt == null || lt.notices.isEmpty()) {
				logger.info("delete notice " + notice.getSourceUrl());
				notice.setType(Notice.TYPE_DELETED);
			} else {
				notice.setTitle(lt.notices.get(0).getTitle());
				notice.setContent(lt.notices.get(0).getContent());
				notice.setHot(lt.notices.get(0).getHot());
				notice.setDate(lt.notices.get(0).getDate());
				notice.setResourceUrl(lt.notices.get(0).getResourceUrl());
			}
			DataManager.addData(notice);
		}
		resp.getWriter().println("done");
	}

	private static final Logger logger = Logger.getLogger(DeleteServlet.class
			.getName());
	public static final int DELETE_NUM_ONCE = 5;

}
